package com.it.tydic.estate.module.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.it.tydic.estate.common.aop.CodeMsg;
import com.it.tydic.estate.common.aop.Result;
import com.it.tydic.estate.common.util.CommonUtils;
import com.it.tydic.estate.common.util.InstaIdGenerator;
import com.it.tydic.estate.common.util.StringUtils;
import com.it.tydic.estate.common.util.TimeUtil;
import com.it.tydic.estate.module.mapper.inner.TbWxglWxrdjMapper;
import com.it.tydic.estate.module.model.inner.TbWxglWxrdj;
import com.it.tydic.estate.module.service.WxglWxrdjService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.*;

/**
 * 维修管理 维修人员登记
 * Created by jinghongtao on 2020/3/3.
 */
@Transactional
@Service
public class WxglWxrdjServiceImpl implements WxglWxrdjService{

    @Resource
    private TbWxglWxrdjMapper tbWxglWxrdjMapper;
    /****
     * 分页查询 数据列表
     * @param record
     * @return
     */
    @Override
    public Result queryByPage(Map<String,Object> record) {
        Map<String,Object> map = new HashMap<>();
        CommonUtils.pageProcess(record);//验证处理分页
        //record.put("zfbj","0");//查未删的数据
        Page<TbWxglWxrdj> xiaoquguanlis = tbWxglWxrdjMapper.selectLists(record);
        List<TbWxglWxrdj> result = xiaoquguanlis.getResult();
        map.put("result",result);
        map.put("totle",xiaoquguanlis.getTotal());
        return Result.success(map);
    }
    /**
     * 根据主表id 分页查询 所有维修人员登记信息
     * @param
     * @return
     */
    @Override
    public  Result getAllData(Map<String,Object> record) {
        CommonUtils.pageProcess(record);//验证处理分页
        Map<String,Object> map = new HashMap<>();
        if("".equals(record.get("djbid")) || null==record.get("djbid")){
            return   Result.error(CodeMsg.EMPTY_PARAM_ERROR);
        }else {
            //record.put("zfbj","0");
            Page<TbWxglWxrdj> wxrdjList = tbWxglWxrdjMapper.selectLists(record);
            List<TbWxglWxrdj> result =wxrdjList.getResult();
            map.put("result",result);
            map.put("totle",wxrdjList.getTotal());
            return Result.success(map);
        }

    }
    /**
     * 根据id 查询单条记录
     * @param id
     * @return
     */
    @Override
    public  Result selectByPrimaryKey(String id) {
        if(StringUtils.isNotEmpty(id)){
            TbWxglWxrdj tbWxglWxrdj = tbWxglWxrdjMapper.selectByPrimaryKey(id);
            return Result.success(tbWxglWxrdj);
        }else {
            return   Result.error(CodeMsg.EMPTY_PARAM_ERROR);
        }
    }
    /**
     * 根据id 删除
     * @param id
     * @return
     */
    @Override
    public Result deleteById(String id){
        if(StringUtils.isNotEmpty(id)){
            int i = tbWxglWxrdjMapper.deleteByPrimaryKey(id);
            if(1 == i){
                return   Result.success(CodeMsg.SUCCESS);
            }else {
                return   Result.error(CodeMsg.DELETE_ERROR);
            }
        }else {
            return   Result.error(CodeMsg.EMPTY_PARAM_ERROR);
        }
    }
    /**
     * 根据id 逻辑删除
     * @param id
     * @return
     */
    @Override
    public  Result deleteUp(String id){
        if(StringUtils.isNotEmpty(id)){
            int i = tbWxglWxrdjMapper.deleteUp(id);
            if(1 == i){
                return   Result.success(CodeMsg.SUCCESS);
            }else {
                return   Result.error(CodeMsg.DELETE_ERROR);
            }
        }else{
            return   Result.error(CodeMsg.EMPTY_PARAM_ERROR);
        }
    }
    /**
     *  批量逻辑删除
     * @param ids
     * @return
     */
    @Override
    public Result deleteByIds(String[] ids){
        if (ids.length == 0) {
            return  Result.error(CodeMsg.EMPTY_PARAM_ERROR,null);
        }
        int result = tbWxglWxrdjMapper.deleteByList(ids);
        if (result == ids.length) {
            return  Result.success(CodeMsg.SUCCESS);
        }
        return Result.error(CodeMsg.DELETE_ERROR, null);
    }
    /**
     * 根据id 更新
     * @param record
     * @return
     */
    @Override
    public  Result updateData(TbWxglWxrdj record){
        if(null!=record){
            //record.setBdczsj(new Date());//报单操作时间
            //record.setBdczry("");//报单操作人员
            int i = tbWxglWxrdjMapper.updateByPrimaryKeySelective(record);
            if(1 == i){
                return  Result.success(CodeMsg.SUCCESS);
            }else {
                return Result.error(CodeMsg.UPDATE_ERROR,null);
            }
        }else {
            return  Result.error(CodeMsg.EMPTY_PARAM_ERROR,null);
        }
    }
    /**
     * 保存
     * @param record
     * @return
     */
    @Override
    public Result saveDate(TbWxglWxrdj record){
        if(null == (record.getZjbid()) || "".equals(record.getZjbid())){
            long id = new InstaIdGenerator().nextId(TimeUtil.timeGen());
            record.setZjbid(String.valueOf(id));
            //record.setCzsj(new Date());//报单操作时间
            //record.setCzr("");//报单操作人员
            //record.setZfbj("0");//删除标记：0正常，1 删除
            int i = tbWxglWxrdjMapper.insertSelective(record);
            if(1 == i){
                return  Result.success(record);
            }else {
                return  Result.error(CodeMsg.INSERT_ERROR,null);
            }
        }else {
            return  Result.error(CodeMsg.INTER_ERROR,null);
        }

    }
    /**
     * 批量保存或修改
     * @param params
     * @return
     */
    @Override
    public Result saveList(List<TbWxglWxrdj> params,String id){
        //List<Map<String,Object>> ryxxList = (List<Map<String,Object>>)params.get("ryxxList");
        int i = 0;
        if(null != params && 0<params.size()){
            List<TbWxglWxrdj> insetList =new ArrayList<>();//要新增的集合
            List<TbWxglWxrdj> updateList =new ArrayList<>();//要修改的集合
            for(TbWxglWxrdj record:params){
                if(null==record.getZjbid() || "".equals(record.getZjbid())){
                    record.setDjbid(id);//将主表id
                    long zjbid = new InstaIdGenerator().nextId(TimeUtil.timeGen());
                    record.setZjbid(String.valueOf(zjbid));
                    insetList.add(record);
                }else{
                    //record.setDjbid(id);//将主表id
                    updateList.add(record);
                }
            }
            if(0<insetList.size()){
               i = tbWxglWxrdjMapper.insertByList(insetList);
            }
            if(0<updateList.size()){
                i = tbWxglWxrdjMapper.updateByList(updateList);
            }

            if(0 < i){
                return  Result.success(id);
            }else {
                return  Result.error(CodeMsg.UPDATE_ERROR,null);
            }
        }else {
            return  Result.error(CodeMsg.EMPTY_PARAM_ERROR,null);
        }

    }

}
